$(document).ready(function()
    {
        $.table_master = null;
        var selectID = null;
        
        $.init_master_non_toko = function()
        {
            $.ajax({
                url: base_url + 'br-02/barang/get_all',
                type: 'post',
                success : function(data)
                {
                    $.table_master = $('#barang_master').dataTable({
                        "bJQueryUI": true,
                        "sPaginationType": "full_numbers",
                        "aaData": data.data,
                        "bDestroy": true,
                        "sScrollX": "100%",
                        "sScrollXInner": "110%",
                        "bScrollCollapse": true,
                        "aoColumns":[
                        {
                            'sTitle' : 'Kode Barang',
                            'mDataProp' : 'KODE_BARANG'
                        },
                        {
                            'sTitle' : 'Jenis Barang',
                            'mDataProp' : 'NAMA_JENIS_BARANG'
                        },
                        {
                            'sTitle' : 'Nama',
                            'mDataProp' : 'NAMA_BARANG'
                        },
                        {
                            'sTitle' : 'Merk',
                            'mDataProp' : 'MERK'
                        },
                        {
                            'sTitle' : 'Satuan',
                            'mDataProp' : 'NAMA_SATUAN'
                        },
			{
                            'sTitle' : 'Pilihan',
                            'mDataProp' : 'KODE_BARANG'
                        },
                        ],
                        "fnRowCallback": function( nRow, aData, iDisplayIndex )
                        {
                            $('td:last', nRow).html('<div style="text-align:center;"> <ul class="action-buttons clearfix"> \n\
                                                            <li> <a class="button button-gray no-text" href="#" onClick="$.update_barang_form(\''+ $('td:eq(0)', nRow).html() +'\')"> <span class="pencil"></span> </a> </li> \n\
                                                            <li> <a class="button button-gray no-text" href="#" onClick="$.delete_barang(\''+ $('td:eq(0)', nRow).html() +'\')"> <span class="bin"></span> </a> </li> \n\
                                                        </ul></div>');
                            return nRow;
                        }
                    });
                },
                error: function(res)
                {
                    alert(res.responseText);
                },
                dataType: 'json'
            });
        }

        $.init_master_toko = function()
        {
            $.ajax({
                url: base_url + 'br-02/barang/get_all/' + $("#ID_TOKO").val(),
                type: 'post',
                success : function(data)
                {
                    $.table_master = $('#barang_master').dataTable({
                        "bJQueryUI": true,
                        "sPaginationType": "full_numbers",
                        "aaData": data.data,
                        "bDestroy": true,
                        "sScrollX": "100%",
                        "sScrollXInner": "110%",
                        "bScrollCollapse": true,
                        "aoColumns":[
                        {
                            'sTitle' : 'Kode Barang',
                            'mDataProp' : 'KODE_BARANG'
                        },
                        {
                            'sTitle' : 'Pemilik Barang',
                            'mDataProp' : 'PEMILIK_BARANG'
                        },
                        {
                            'sTitle' : 'Nama',
                            'mDataProp' : 'NAMA_BARANG'
                        },
                        {
                            'sTitle' : 'Merk',
                            'mDataProp' : 'MERK'
                        },
                        {
                            'sTitle' : 'Satuan',
                            'mDataProp' : 'NAMA_SATUAN'
                        },
			{
                            'sTitle' : 'Pilihan',
                            'mDataProp' : 'KODE_BARANG'
                        },
                        ],
                        "fnRowCallback": function( nRow, aData, iDisplayIndex )
                        {
                            $('td:last', nRow).html('<div style="text-align:center;"> <ul class="action-buttons clearfix"> \n\
                                                            <li> <a class="button button-gray no-text" href="#" onClick="$.update_barang_form(\''+ $('td:eq(0)', nRow).html() +'\')"> <span class="pencil"></span> </a> </li> \n\
                                                            <li> <a class="button button-gray no-text" href="#" onClick="$.delete_barang(\''+ $('td:eq(0)', nRow).html() +'\')"> <span class="bin"></span> </a> </li> \n\
                                                        </ul></div>');
                            return nRow;
                        }
                    });
                },
                error: function(res)
                {
                    alert(res.responseText);
                },
                dataType: 'json'
            });
        }

        $.add_barang = function()
        {
            if($("#FILTER_MODE_TOKO").attr('checked'))
                $.call_ajax('br-02/barang/add/' + $("#ID_TOKO").val(), "#barang_form", null);
            else
                $.call_ajax('br-02/barang/add/', "#barang_form", null);
            $('#dialog-barang-form').dialog('close');
            $.init_master();
        }

        $.update_barang = function()
        {
            if($("#FILTER_MODE_TOKO").attr('checked'))
                $.call_ajax('br-02/barang/update/' + selectID + "/" + $("#ID_TOKO").val(), "#barang_form", null);
            else
                $.call_ajax('br-02/barang/update/' + selectID, "#barang_form", null);
            $('#dialog-barang-form').dialog('close');
            $.init_master();
        }       

        $.delete_barang = function(id)
        {
            if(!confirm("Apakah anda yakin akan menghapus data ini ?")) return false;
            $.call_ajax('br-02/barang/delete/' + id, null, null);
            $.init_master();
        }

        $('#dialog-barang-form').dialog({
            title: 'Data barang',
            width: '950px',
            autoOpen:false,
            modal:true,
            resizable:false
        });

        $.update_barang_form = function(id)
        {
            if($("#FILTER_MODE_TOKO").attr('checked'))
                $.call_ajax('br-02/barang/get/' + id + "/" + $("#ID_TOKO").val(), null, "#barang_form");
            else
                $.call_ajax('br-02/barang/get/' + id, null, "#barang_form");

            selectID = id;
            $.init_input();
            $("#update_submit").show();
            $("#add_submit").hide();
            $('#dialog-barang-form').dialog('open');
        }

        $.add_barang_form = function()
        {
            $.clear_form("barang_form");
            $.init_input();
            $("#add_submit").show();
            $("#update_submit").hide();
            $('#dialog-barang-form').dialog('open');
        }
        
        $.init_input = function()
        {
            if($("#FILTER_MODE_TOKO").attr('checked')) //Toko
            {
                $("#INPUT_TOKO").show();
                $(".toko").show();
                $(".non-toko").hide();

                //$.init_master_non_toko();
            }
            else // Non Toko
            {
                $("#INPUT_TOKO").hide();
                $(".toko").hide();
                $(".non-toko").show();
                //$.init_master_toko();
            }
        }

        $.init_master = function()
        {
            if(!$("#FILTER_MODE_TOKO").attr('checked'))
            {
                $("#FILTER_TOKO").hide(1000);
                $.init_master_non_toko();
            }
            else
            {
                $("#FILTER_TOKO").show(1000);
                $.init_master_toko();
            }
        }
        

        $("#barang_form").submit(function() {
            return false;
        });
        
        $("#barang_add").click($.add_barang_form);
        $("#add_submit").click($.add_barang);
        $("#update_submit").click($.update_barang);
        $("#FILTER_MODE_TOKO").change($.init_master);
        $("#FILTER_MODE_TOKO").attr('checked', true);
        $.init_master();
    });